package com.shadow.bus.main.work;

import android.util.SparseArray;

import com.alibaba.fastjson.TypeReference;
import com.shadow.bus.config.ItemFiled;
import com.shadow.bus.config.ItemType;
import com.shadow.bus.entity.PersonEntity;
import com.shadow.bus.entity.PostRepairInfo;
import com.shadow.bus.entity.RepairEntity;
import com.shadow.bus.entity.TroubleClassificationEntity;
import com.shadow.bus.entity.TroubleEntity;
import com.shadow.bus.entity.TroubleOperationEntity;
import com.tepth.shadow.ui.recycler.MultipleItemEntity;
import com.tepth.shadow.ui.recycler.ObjConverter;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * @author zhangyu
 * @date 2018/7/4
 */

public class WorkDataMultipleConverter {
    public void convert(final String response, final OnConverterListener listener) {
        Observable.create(new ObservableOnSubscribe<Object>() {
            @Override
            public void subscribe(ObservableEmitter<Object> e) throws Exception {
                List<Object> data = new ArrayList<>();
                ArrayList<MultipleItemEntity> itemEntities = new ArrayList<>();
                data.add(itemEntities);
                RepairEntity repairEntity = new ObjConverter<RepairEntity>().
                        convert(response, new TypeReference<RepairEntity>() {
                        });
                List<TroubleEntity> troubleEntities = repairEntity.getListTrouble();
                data.add(troubleEntities);
                data.add(repairEntity);
                SparseArray<List<TroubleEntity>> sparseArray = new SparseArray<>();
                TroubleEntity CustomTrouble = null;
                if (troubleEntities != null) {
                    for (TroubleEntity troubleEntity : troubleEntities) {
                        if (troubleEntity.getId() == 0 || troubleEntity.getName() == null) {
                            continue;
                        }
                        TroubleClassificationEntity classificationEntity =
                                troubleEntity.getClassification();
                        if (classificationEntity != null) {
                            List<TroubleEntity> troubleEntityList = sparseArray.get((int) classificationEntity.getId());
                            if (troubleEntityList == null) {
                                troubleEntityList = new ArrayList<>();
                                sparseArray.put((int) classificationEntity.getId(), troubleEntityList);
                            }
                            troubleEntityList.add(troubleEntity);
                        } else {
                            //自定义
                            CustomTrouble = troubleEntity;
                        }
                    }
                    //将自定义的添加在第一个
                    itemEntities.add(MultipleItemEntity.builder()
                            .setItemType(ItemType.TYPE_ITEM_TROUBLE_HEAD)
                            .setField(ItemFiled.FILED_NAME, "附加作业项")
                            .build());
                    if (CustomTrouble != null) {
                        itemEntities.add(MultipleItemEntity.builder()
                                .setItemType(ItemType.TYPE_ITEM_TROUBLE_CONTENT)
                                .setField(ItemFiled.ITEM_OBJ, CustomTrouble)
                                .build());
                        ArrayList<TroubleOperationEntity> customOperation = CustomTrouble.getListContent();
                        if (customOperation != null) {
                            for (TroubleOperationEntity operationEntity : customOperation) {
                                PersonEntity personEntity = operationEntity.getWorkPerson();
                                operationEntity.setTroubleTypeId(CustomTrouble.getTroubleTypeId());
                                operationEntity.setTroubleId(CustomTrouble.getId());
                                if (personEntity != null) {
                                    if (operationEntity.isRework()) {
                                        itemEntities.add(MultipleItemEntity
                                                .builder()
                                                .setItemType(ItemType.TYPE_ITEM_TROUBLE_OPERATOR_REWORK)
                                                .setField(ItemFiled.ITEM_OBJ, operationEntity)
                                                .setField(ItemFiled.ITEM_TROUBLE_ID, CustomTrouble.getId())
                                                .setField(ItemFiled.ITEM_TROUBLE_NAME, CustomTrouble.getName())
                                                .build());
                                    } else {
                                        itemEntities.add(MultipleItemEntity
                                                .builder()
                                                .setItemType(ItemType.TYPE_ITEM_TROUBLE_OPERATOR_PUNCH)
                                                .setField(ItemFiled.ITEM_OBJ, operationEntity)
                                                .setField(ItemFiled.ITEM_TROUBLE_ID, CustomTrouble.getId())
                                                .setField(ItemFiled.ITEM_TROUBLE_NAME, CustomTrouble.getName())
                                                .build());
                                    }
                                } else {
                                    itemEntities.add(MultipleItemEntity
                                            .builder()
                                            .setItemType(ItemType.TYPE_ITEM_TROUBLE_OPERATOR_WORK)
                                            .setField(ItemFiled.ITEM_OBJ, operationEntity)
                                            .setField(ItemFiled.ITEM_TROUBLE_ID, CustomTrouble.getId())
                                            .setField(ItemFiled.ITEM_TROUBLE_NAME, CustomTrouble.getName())
                                            .build());
                                }
                            }
                        }
                    } else {
                        //构建一个附加作业项
                        CustomTrouble = new TroubleEntity();
                        CustomTrouble.setName("自定义录入@@");
                        CustomTrouble.setId(0);
                        CustomTrouble.setTroubleTypeId(0);
                        itemEntities.add(MultipleItemEntity.builder()
                                .setItemType(ItemType.TYPE_ITEM_TROUBLE_CONTENT)
                                .setField(ItemFiled.ITEM_OBJ, CustomTrouble)
                                .build());
                    }

                    for (int i = 0, nSize = sparseArray.size(); i < nSize; i++) {
                        List<TroubleEntity> troubleEntityList = sparseArray.valueAt(i);
                        itemEntities.add(MultipleItemEntity.builder()
                                .setItemType(ItemType.TYPE_ITEM_TROUBLE_HEAD)
                                .setField(ItemFiled.FILED_NAME, troubleEntityList.get(0).getClassification().getName())
                                .build());
                        for (TroubleEntity troubleEntity : troubleEntityList) {
                            itemEntities.add(MultipleItemEntity.builder()
                                    .setItemType(ItemType.TYPE_ITEM_TROUBLE_CONTENT)
                                    .setField(ItemFiled.ITEM_OBJ, troubleEntity)
                                    .build());
                            List<TroubleOperationEntity> operationEntities = troubleEntity.getListContent();
                            if (operationEntities != null) {
                                for (TroubleOperationEntity entity : operationEntities) {
                                    entity.setTroubleId(troubleEntity.getId());
                                    entity.setTroubleTypeId(troubleEntity.getTroubleTypeId());
                                    PersonEntity personEntity = entity.getWorkPerson();
                                    if (personEntity != null) {
                                        if (entity.isRework()) {
                                            itemEntities.add(MultipleItemEntity
                                                    .builder()
                                                    .setItemType(ItemType.TYPE_ITEM_TROUBLE_OPERATOR_REWORK)
                                                    .setField(ItemFiled.ITEM_OBJ, entity)
                                                    .setField(ItemFiled.ITEM_TROUBLE_ID, troubleEntity.getId())
                                                    .setField(ItemFiled.ITEM_TROUBLE_NAME, troubleEntity.getName())
                                                    .build());
                                        } else {
                                            itemEntities.add(MultipleItemEntity
                                                    .builder()
                                                    .setItemType(ItemType.TYPE_ITEM_TROUBLE_OPERATOR_PUNCH)
                                                    .setField(ItemFiled.ITEM_OBJ, entity)
                                                    .setField(ItemFiled.ITEM_TROUBLE_ID, troubleEntity.getId())
                                                    .setField(ItemFiled.ITEM_TROUBLE_NAME, troubleEntity.getName())
                                                    .build());
                                        }
                                    } else {
                                        itemEntities.add(MultipleItemEntity
                                                .builder()
                                                .setItemType(ItemType.TYPE_ITEM_TROUBLE_OPERATOR_WORK)
                                                .setField(ItemFiled.ITEM_OBJ, entity)
                                                .setField(ItemFiled.ITEM_TROUBLE_ID, troubleEntity.getId())
                                                .setField(ItemFiled.ITEM_TROUBLE_NAME, troubleEntity.getName())
                                                .build());
                                    }
                                }
                            }
                        }
                    }
                }

                e.onNext(data);
            }
        })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Observer<Object>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    @SuppressWarnings("unchecked")
                    public void onNext(Object o) {
                        List<Object> list = (List<Object>) o;
                        if (listener != null) {
                            listener.onConverterAllData((ArrayList<MultipleItemEntity>) list.get(0));
                            listener.onConverterTroubles((List<TroubleEntity>) list.get(1));
                            listener.onConverterRepairInfo((RepairEntity) list.get(2));
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (listener != null) {
                            listener.onConverterError(e.getMessage());
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public PostRepairInfo convert(List<TroubleOperationEntity> list, int busId,
                                  String personNumber,
                                  String personName) {
        PostRepairInfo postRepairInfo = new PostRepairInfo();
        postRepairInfo.setId(busId);
        SparseArray<List<TroubleOperationEntity>> sparseArray = new SparseArray<>();
        for (TroubleOperationEntity entity : list) {
            PersonEntity personEntity = new PersonEntity();
            personEntity.setCode(personNumber);
            personEntity.setName(personName);
            entity.setWorkPerson(personEntity);
            ArrayList<TroubleEntity> children = entity.getChildren();
            if (children != null && !children.isEmpty()) {
                for (TroubleEntity troubleEntity : children) {
                    List<TroubleOperationEntity> operationEntities =
                            sparseArray.get((int) troubleEntity.getId());
                    if (operationEntities == null) {
                        operationEntities = new ArrayList<>();
                        sparseArray.put((int) troubleEntity.getId(), operationEntities);
                    }
                    TroubleOperationEntity entityCopy = entity.copy();
                    entityCopy.setTroubleTypeName(troubleEntity.getName());
                    entityCopy.setTroubleId(troubleEntity.getId());
                    entityCopy.setUseCount(0);
                    entityCopy.setTroubleTypeId(troubleEntity.getTroubleTypeId());
                    entityCopy.setClassificationName(troubleEntity.getClassification().getName());
                    entityCopy.setClassificationId(troubleEntity.getClassification().getId());
                    operationEntities.add(entityCopy);
                }
            }
            List<TroubleOperationEntity> operationEntities = sparseArray.get((int) entity.getTroubleId());
            if (operationEntities == null) {
                operationEntities = new ArrayList<>();
                sparseArray.put((int) entity.getTroubleId(), operationEntities);
            }
            operationEntities.add(entity.copy());
        }
        List<PostRepairInfo.ListTroubleBean> ListTrouble = new ArrayList<>();
        for (int i = 0, nSize = sparseArray.size(); i < nSize; i++) {
            List<TroubleOperationEntity> operationEntities = sparseArray.valueAt(i);
            PostRepairInfo.ListTroubleBean listTroubleBean = new PostRepairInfo.ListTroubleBean();
            listTroubleBean.setId(operationEntities.get(0).getTroubleId());
            listTroubleBean.setName(operationEntities.get(0).getTroubleTypeName());
            listTroubleBean.setListContent(operationEntities);
            ListTrouble.add(listTroubleBean);
        }
        postRepairInfo.setListTrouble(ListTrouble);
        return postRepairInfo;
    }


    public interface OnConverterListener {
        void onConverterAllData(ArrayList<MultipleItemEntity> list);

        void onConverterTroubles(List<TroubleEntity> troubleEntities);

        void onConverterRepairInfo(RepairEntity repairEntity);

        void onConverterError(String msg);
    }
}
